package com.controller;

import com.entity.Phone;
import com.resp.BaseResponseEntity;
import com.service.IPhoneService;
import com.service.impl.PhoneServiceimpl;
import com.utlils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加手机
 */
@WebServlet("/AddPhone")
public class AddPhoneController extends HttpServlet {
    IPhoneService service=new PhoneServiceimpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String name = req.getParameter("name");
        String color = req.getParameter("color");
        String image = req.getParameter("image");
        String str = req.getParameter("state");
        String strprice = req.getParameter("price");
        String value = req.getParameter("value");

        int state = Integer.parseInt(str);
        int val = Integer.parseInt(value);
        int price = Integer.parseInt(strprice);

        Phone phone=new Phone(name,color,price,image,state,val);
        System.out.println(phone+"保存手机信息接收到的手机信息");
        Integer integer = service.AddPhone(phone);

        BaseResponseEntity entity=null;
        if(integer>0){
            entity=new BaseResponseEntity(200,"保存成功",integer);
        }else {
            entity= BaseResponseEntity.error();
        }
        ResponseUtils.responseToJson(resp,entity);
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
